<?php
class albummodel{
		var $db;
		var $base;
		function albummodel(&$base){
			$this->base = $base;
			$this->db = $base->db;
		}
		function checkpermission($albumid,$userid)
		{
			$result = $this->db->fetch_first("SELECT album_id FROM ".AM_DBTABLEPRE."albums WHERE album_id={$albumid} AND user_id={$userid}");
			return $result;
		}
	function create($row){
		$reslut = $this->db->query("INSERT INTO ".AM_DBTABLEPRE."albums VALUES('','{$row['user_id']}','{$row['title']}','{$row['description']}','{$row['created']}','{$row['updatetime']}','{$row['url']}','0','0','0')");
		return $this->db->insert_id();
	}
	function albumlist($id){
	$result = $this->db->fetch_all("SELECT album_id,albumname,thumburl,photos_count FROM ".AM_DBTABLEPRE."albums WHERE user_id = $id");
	return empty($result)?0:$result;
	}
	function get_uid($albumid)
	{
		$result = $this->db->fetch_first("SELECT user_id FROM ".AM_DBTABLEPRE."albums WHERE album_id = $albumid");
		return $result['user_id'];
	}
	function delete($albumid,$uid)
	{
		$result = $this->db->query("DELETE FROM ".AM_DBTABLEPRE."albums WHERE album_id={$albumid} AND user_id={$uid}");
		return $this->db->affected_rows();
	}
	function albumdata($albumid)
	{
		$result = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."albums WHERE album_id={$albumid}");
		return $result;
	}
	function update($row,$albumid)
	{
		$sql = 'UPDATE '.AM_DBTABLEPRE.'albums SET ';
		foreach($row as $key=>$value)
		{
			$sql.="$key".' = '."$value".',';
		}
		$sql = substr($sql,0,-1)." WHERE album_id={$albumid}";
		$result = $this->db->query($sql);
		return $result;
	}
	function get_hot($from,$num)
	{
		$result = $this->db->fetch_all("SElECT * FROM ".AM_DBTABLEPRE."albums ORDER BY aviewnum AND acomment_num DESC LIMIT {$from},{$num}");
		return $result;
	}
	function get_count()
	{
		$result = $this->db->fetch_first("SElECT COUNT(*) FROM ".AM_DBTABLEPRE."albums");
		return $result['COUNT(*)'];
	}	
}
?>